//
//  LikeViewController.m
//  project
//
//  Created by lanou on 15/11/7.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "LikeViewController.h"
#import "HotListCell.h"
#import "FMDatabase.h"
#import "ListModel.h"
#import "HotDetailViewController.h"

@interface LikeViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)NSMutableArray *dataArray;
@property (nonatomic, strong)FMDatabase *dataBase;

@end

@implementation LikeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.backgroundColor = [UIColor magentaColor];
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

    
    
    //self.view.backgroundColor = [[UIColor alloc]initWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1];
    
  
    UILabel *label = [[UILabel alloc]init];
    label.frame = CGRectMake((kWidth - 100)/2, 30, 100, 20);
    label.text = @"喜欢的礼物";
    label.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = label;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    CGFloat item = (kWidth - 30)/2;
    layout.itemSize = CGSizeMake(item, item + 80);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 10, kWidth, kHeight - 50) collectionViewLayout:layout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = [[UIColor alloc]initWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1];
    
    [_collectionView registerClass:[HotListCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:_collectionView];
    _dataArray = [NSMutableArray array];
    
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"project.db"];
    
    _dataBase = [FMDatabase databaseWithPath:filePath];
    
    [_dataBase open];
       FMResultSet *resultSet = [_dataBase executeQueryWithFormat:@"select * from StragryLove"];
    while ([resultSet next])
    {
        NSString *stragryimage = [resultSet stringForColumn:@"stragryimage"];
        NSString *stragryname = [resultSet stringForColumn:@"stragryname"];
        NSString *stragryid = [resultSet stringForColumn:@"stragryid"];
        NSString *stragryurl = [resultSet stringForColumn:@"stargryurl"];
        NSString *stragrylikecount = [resultSet stringForColumn:@"stragrylikecount"];
        NSString *price = [resultSet stringForColumn:@"price"];
        ListModel *model = [[ListModel alloc]init];
        model.cover_image_url = stragryimage;
        model.name = stragryname;
        model.idd = stragryid;
        model.url = stragryurl;
        model.favorites_count = [NSNumber numberWithInt:(int) stragrylikecount];
        model.price = price;
        
        if (model != nil)
        {
            [self.dataArray addObject:model];

        }
    }
    
    if (_dataArray.count != 0)
    {
        [self.collectionView reloadData];
    }
    

    
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HotListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    [cell ConfigModel:self.dataArray[indexPath.row]];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HotDetailViewController *hotDetail = [[HotDetailViewController alloc]init];
    hotDetail.model = self.dataArray[indexPath.item];
    hotDetail.cancelBlock = ^(ListModel *model)
    {
        [_dataArray removeObject:model];
        [self.collectionView reloadData];
        
    };
    [self.navigationController pushViewController:hotDetail animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
